﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using NotAClue.ComponentModel.DataAnnotations;
using C1.Web.Wijmo.Juice.WijAccordion;

namespace DD_Extensions
{
    public partial class WijmoAccordianTemplate : System.Web.DynamicData.EntityTemplateUserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            var groupAttribute = Table.GetAttribute<GroupNamesAttribute>();
            if (groupAttribute == null)
                throw new InvalidOperationException("A GroupsAttribute is required for AJAX tab group to work.");

            var row = new HtmlTableRow();
            var td = new HtmlTableCell();
            row.Controls.Add(td);
            this.Controls.Add(row);
            // create tab container to hold each children column
            var panel = new Panel();
            panel.ID = "tabContainer_" + Table.Name;

            //<div>Step 1</div>
            //<div>
            //    <h1>Step 1</h1>
            //    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin ac lacus ac nibh
            //    viverra faucibus. Mauris non vestibulum dui</div>
            //<div>

            // add DIV for each group
            foreach (var gi in groupAttribute.Groups)
            {
                var groupName = gi.Value;

                // create title DIV
                var divTitle = new HtmlGenericControl("div");
                divTitle.InnerText = groupName;

                // add the title DIV to panel
                panel.Controls.Add(divTitle);

                var divContent = new HtmlGenericControl("div");

                // get columns for this group
                var columns = from c in Table.GetScaffoldColumns(Mode, ContainerType)
                              where c.GetAttributeOrDefault<GroupAttribute>().Index == gi.Key
                              orderby c.GetAttributeOrDefault<DisplayAttribute>().GetOrder()
                              select c;

                // add table for this tabs fields
                var htmlTable = new HtmlTable();
                htmlTable.Attributes.Add("class", "DDDetailsTable");
                htmlTable.CellPadding = 6;
                htmlTable.Attributes.Add("Name", groupName);

                // add fields
                foreach (MetaColumn column in columns)
                {
                    // new row
                    var htmlRow = new HtmlTableRow();
                    htmlTable.Controls.Add(htmlRow);

                    // add header cell
                    var tdHeader = new HtmlTableCell();
                    tdHeader.Attributes.Add("class", "DDLightHeader");
                    tdHeader.InnerText = column.DisplayName;
                    // add cell to row
                    htmlRow.Controls.Add(tdHeader);

                    // add data cell
                    var tdData = new HtmlTableCell();
                    // get field template
                    var dynamicControl = new DynamicControl()
                        {
                            Mode = Mode,
                            DataField = column.Name,
                            ValidationGroup = this.ValidationGroup
                        };
                    // add field template to cell
                    tdData.Controls.Add(dynamicControl);
                    // add cell to row
                    htmlRow.Controls.Add(tdData);
                }

                // add the DynamicControl to the tab panel
                divContent.Controls.Add(htmlTable);

                // add the DIV to panel
                panel.Controls.Add(divContent);
            }


            // add the panel to the page
            td.Controls.Add(panel);

            // associate the panel with the Wijmo tabs
            var wijAccordion1 = new WijAccordion()
                {
                    ID = "wijAccordion1",
                    TargetControlID = panel.ID
                };

            // add Wijmo tabs to the page
            td.Controls.Add(wijAccordion1);
        }

        protected void Label_PreRender(object sender, EventArgs e)
        {
            Label label = (Label)sender;
            DynamicControl dynamicControl = (DynamicControl)label.FindControl("DynamicControl");
            FieldTemplateUserControl ftuc = dynamicControl.FieldTemplate as FieldTemplateUserControl;

            if (ftuc != null && ftuc.DataControl != null)
                label.AssociatedControlID = ftuc.DataControl.GetUniqueIDRelativeTo(label);
        }

        public class _NamingContainer : Control, INamingContainer { }
    }
}